#include "serverconnection.h"

ServerConnection::ServerConnection(QHostAddress a, quint16 p, bool onlyReceive,Server*server) : ConnectionPlayer(a,p,onlyReceive)
{
    parentServer = server;
    score = 0;
}

ServerConnection::ServerConnection(bool onlyReceive, Server*server) : ConnectionPlayer(onlyReceive)
{
    parentServer = server;
}

Server* ServerConnection::server()
{
    return parentServer;
}

void ServerConnection::runCommand(PacketData* data, bool isNewestPacket)
{
    QString command = data->nextString();
    //qDebug() << "Client says: " <<command;
    if (command == "HELLO") doHELLO(data);
    else if (command == "DISCONNECT") doDISCONNECT(data);
    else if (command == "STATE") doSTATE(data);
    else if (command == "SHOOT") doSHOOT(data);
    //delete data;
}

void ServerConnection::doHELLO(PacketData* args)
{
    QString nick = args->nextString();
    setNick(nick);
    setScore(0);
    PacketData* toSend = new PacketData();
    toSend->appendString("HELLO");
    toSend->appendInt(getPlayerId());
    sendPacket(toSend,true);

    emit newPlayerConnected(this);
    connectionState = QAbstractSocket::ConnectedState;
    emit connectionStateChanged(connectionState);

    PacketData* toSend2 = new PacketData();
    toSend2->appendString("LIST");
    QHashIterator<QString,ServerConnection*> it(server()->getConnectionHash());
    while (it.hasNext())
    {
        it.next();
        ServerConnection* conn = it.value();
        if (conn != this)
        {
            toSend2->appendInt(conn->getPlayerId());
            toSend2->appendString(conn->getNick());
            toSend2->appendInt(conn->getScore());
        }
    }
    sendPacket(toSend2, true);

    PacketData* toSend3 = new PacketData();
    toSend3->appendString("JOIN");
    toSend3->appendInt(getPlayerId());
    toSend3->appendString(getNick());
    QVector<ServerConnection*> *except = new QVector<ServerConnection*>();
    except->append(this);
    server()->sendToAll(toSend3, true,true, except);
    delete except;
}

void ServerConnection::doSTATE(PacketData* data)
{
	qreal x = data->nextReal();
	qreal y = data->nextReal();
	qint32 d1 = data->nextInt();
	qint32 d2 = data->nextInt();
	qint32 angle = data->nextInt();
	qint32 isReloading = data->nextInt();

	setPosition(QPointF(x,y));
	setDirs(d1,d2);
	setAngle(angle);
	setIsReloading(isReloading==1);
}

void ServerConnection::doSHOOT(PacketData* data)
{
	qreal x = data->nextReal();
	qreal y = data->nextReal();
	qint32 angle = data->nextInt();
	qDebug() << "remote player shoot";
	fireGun(x,y,qreal(angle));
	PacketData* toSend = new PacketData();
	toSend->appendString("SHOOT");
	toSend->appendInt(getPlayerId());
	toSend->appendReal(x);
	toSend->appendReal(y);
	toSend->appendInt(angle);
	QVector<ServerConnection*> *except = new QVector<ServerConnection*>();
	except->append(this);

	server()->sendToAll(toSend, true, false, except);
	delete except;
}

void ServerConnection::doDISCONNECT(PacketData* args)
{
    disconnFromHost();
}
